// ==UserScript==
// @name         生成二维码
// @namespace    wxc
// @version      0.1
// @description  生成二维码
// @author       wxc
// @include      *
// @run-at       document-start
// @grant        unsafeWindow
// @require      https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/tingle/0.15.3/tingle.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/keymaster/1.6.1/keymaster.min.js
// @resource tinglecss https://cdnjs.cloudflare.com/ajax/libs/tingle/0.15.3/tingle.min.css
// ==/UserScript==

(function() {
  'use strict';
  let isCssLoad = false;
  let action = async function(){
    if(!isCssLoad) {
       function loadCss(href) {
         return new Promise((resolve, reject) => {
           var link = document.createElement('link');
           link.rel = 'stylesheet';
           link.href = href;
           link.addEventListener('load', function () {
              isCssLoad = false;
               resolve();
             }, false);
           document.body.appendChild(link);
           let style = document.createElement("style");
           style.type = "text/css";
           let textNode = document.createTextNode('.tingle-modal-box { width:352px }');
           style.appendChild(textNode);
           document.body.appendChild(style);
         })
       }
      await loadCss('https://cdnjs.cloudflare.com/ajax/libs/tingle/0.15.3/tingle.min.css');
    }
    let modal = new tingle.modal({
        closeMethods: ['overlay', 'button', 'escape'],
        closeLabel: "Close",
        cssClass: ['custom-class-1', 'custom-class-2'],
        onClose: function() {
            modal.destroy();
        },
    });
    modal.setContent(`<div class='__gm__qrcode__'/>`);
    new QRCode(document.querySelector(".__gm__qrcode__"), window.location.href );  // 设置要生成二维码的链接

    modal.open();
  };
  key('ctrl+q', action);
})();